import mysql.connector
from pydantic import BaseModel
#向数据库存入相关数据
class Student(BaseModel):
    stuid: str=""
    stuName: str=""
    gender: str=""
    stuAge: str=""
    fromplace: str =""
    college: str=""
    isGZ: str=""

def dataIn(student):  #录入数据
    mydb = mysql.connector.connect(
    host="124.222.244.117",    
    user="zrgj9",   
    passwd="zrgj9",
    database="zrgj9"
    )
    if student.fromplace == "2-0":
        student.fromplace = "省外"
        student.isGZ = "省外"
    elif student.fromplace == "1-1":
        student.fromplace = "贵阳市"
        student.isGZ = "贵州"
    elif student.fromplace == "1-2":
        student.fromplace = "遵义市"
        student.isGZ = "贵州"
    elif student.fromplace == "1-3":
        student.fromplace = "安顺市"
        student.isGZ = "贵州"
    elif student.fromplace == "1-4":
        student.fromplace = "六盘水市"
        student.isGZ = "贵州"
    elif student.fromplace == "1-5":
        student.fromplace = "毕节市"
        student.isGZ = "贵州"
    elif student.fromplace == "1-6":
        student.fromplace = "铜仁市"
        student.isGZ = "贵州"
    elif student.fromplace == "1-7":
        student.fromplace = "黔西南州"
        student.isGZ = "贵州"
    elif student.fromplace == "1-8":
        student.fromplace = "黔东南州"
        student.isGZ = "贵州"
    elif student.fromplace == "1-9":
        student.fromplace = "黔南"
        student.isGZ = "贵州"
   
    mycursor = mydb.cursor()
    try:
        sql = "UPDATE student SET stuName = %s WHERE stuid = %s"
        val = (student.stuName,student.stuid)
        mycursor.execute(sql,val)
        
        sql = "UPDATE student SET stuAge = %s WHERE stuid = %s"
        val = (student.stuAge,student.stuid)
        mycursor.execute(sql,val)
       
        sql = "UPDATE student SET gender = %s WHERE stuid = %s"
        val = (student.gender,student.stuid)
        mycursor.execute(sql,val)
        
        sql = "UPDATE student SET fromplace = %s WHERE stuid = %s"
        val = (student.fromplace,student.stuid)
        mycursor.execute(sql,val)
        
        sql = "UPDATE student SET college = %s WHERE stuid = %s"
        val = (student.college,student.stuid)
        mycursor.execute(sql,val)

        sql = "UPDATE student SET register = %s WHERE stuid = %s"
        val = (1,student.stuid)
        mycursor.execute(sql,val)

        sql = "UPDATE student SET isGZ = %s WHERE stuid = %s"
        val = (student.isGZ,student.stuid)
        mycursor.execute(sql,val)
         
        mydb.commit() 
    except:
        mydb.rollback()   #数据库回滚（rollback）
    mydb.close()
    return mycursor.rowcount

